package com.itheima.apis.subject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.model.common.dtos.PageResponseResult;
import com.car.model.common.dtos.ResponseResult;
import com.car.model.common.enums.AppHttpCodeEnum;
import com.car.model.subject.dtos.FacilitatorDto;
import com.car.model.subject.pojos.Facilitator;
import com.itheima.apis.subject.mapper.FacilitatorMapper;
import com.itheima.apis.subject.service.FacilitatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class FacilitatorServiceImpl extends ServiceImpl<FacilitatorMapper, Facilitator> implements FacilitatorService {
    @Autowired
    public RedisTemplate redisTemplate;
    //条件查询
    @Override
    public ResponseResult findPage(FacilitatorDto dto) {
        //1.参数检测
        if (dto == null) {
            dto = new FacilitatorDto();
        }
        dto.checkParam();
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<Facilitator> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getCode() != null, Facilitator::getCode, dto.getCode())
                .eq(dto.getSimple_name() != null, Facilitator::getSimple_name, dto.getSimple_name())
                .eq(dto.getProvince() != null, Facilitator::getProvince, dto.getProvince())
                .eq(dto.getCity() != null, Facilitator::getCity, dto.getCity())
                .eq(dto.getCounty() != null, Facilitator::getCounty, dto.getCounty())
                .eq(dto.getContact_phone() != null, Facilitator::getContact_phone, dto.getContact_phone())
                .eq(dto.getIs_chain() != null, Facilitator::getIs_chain, dto.getIs_chain())
                .eq(dto.getContact_phone() != null, Facilitator::getContact_phone, dto.getContact_phone())
                .eq(dto.getStatus() != null, Facilitator::getStatus, dto.getStatus());
        IPage iPage = page(page, wrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        result.setData(iPage.getRecords());
        return ResponseResult.okResult(result);
    }

    @Override
    public ResponseResult insert(Facilitator facilitator) {
        //1.检查参数
        if (null == facilitator) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存
        Long code = redisTemplate.opsForValue().increment("code");
        if (code < 100000) {
            redisTemplate.opsForValue().set("code", 100000);
            code = redisTemplate.opsForValue().increment("code");
        }

        //主体编码
        facilitator.setCode(code + "");
        // 默认为正常状态
        facilitator.setStatus("1");
        save(facilitator);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(Facilitator facilitator) {
        //1.检查参数
        if (null == facilitator || facilitator.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.修改
        updateById(facilitator);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
